package com.example.k.littletag.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import com.example.k.littletag.R;
import com.example.k.littletag.utils.Note;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Kong on 2015/9/29.
 */
public class FeedAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {

    private Context context;
    private List<Note> items;
    private OnRecyclerViewItemClickListener onRecyclerViewItemClickListener = null;

    public FeedAdapter(Context context, List<Note> items) {
        this.context = context;
        this.items = items;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final View view = LayoutInflater.from(context).inflate(R.layout.item_feed, parent, false);
        final CellFeedViewHolder cellFeedViewHolder = new CellFeedViewHolder(view);
        view.setOnClickListener(this);
        return cellFeedViewHolder;
    }

    public void addItem(Note note, int position) {
        items.add(position, note);
        notifyItemInserted(position);
    }

    public void removeItem(Note note) {
        int position = items.indexOf(note);
        items.remove(position);
        notifyItemRemoved(position);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        holder.itemView.setTag(position);
        Note n = items.get(position);
        long[] timeEnd = timeBetween(n.getTime());

        Log.d("time[][][]content", n.getContent());
        Log.d("time[][][]", timeEnd[0] + " " + timeEnd[1] + " " + timeEnd[2] + " " + n.getIsFinish());
        if (n.getIsFinish() == 1) {
            ((CellFeedViewHolder) holder).timeEndDay.setVisibility(View.INVISIBLE);
            ((CellFeedViewHolder) holder).timeEndHour.setVisibility(View.INVISIBLE);
            ((CellFeedViewHolder) holder).timeEndMinute.setVisibility(View.INVISIBLE);
            ((CellFeedViewHolder) holder).time_end_Day.setVisibility(View.INVISIBLE);
            ((CellFeedViewHolder) holder).time_end_hour.setVisibility(View.INVISIBLE);
            ((CellFeedViewHolder) holder).time_end_last.setVisibility(View.INVISIBLE);

            ((CellFeedViewHolder) holder).time_end_minute.setText("已完成");
            ((CellFeedViewHolder) holder).time_end_minute.setTextColor(context.getResources().getColor(R.color.reveal_green));

        } else if (timeEnd[0] == -1 && timeEnd[1] == -1 && timeEnd[2] == -1) {
            ((CellFeedViewHolder) holder).timeEndDay.setVisibility(View.INVISIBLE);
            ((CellFeedViewHolder) holder).timeEndHour.setVisibility(View.INVISIBLE);
            ((CellFeedViewHolder) holder).timeEndMinute.setVisibility(View.INVISIBLE);
            ((CellFeedViewHolder) holder).time_end_Day.setVisibility(View.INVISIBLE);
            ((CellFeedViewHolder) holder).time_end_hour.setVisibility(View.INVISIBLE);
            ((CellFeedViewHolder) holder).time_end_last.setVisibility(View.INVISIBLE);

            ((CellFeedViewHolder) holder).time_end_minute.setText("待办");
            ((CellFeedViewHolder) holder).time_end_minute.setTextColor(context.getResources().getColor(R.color.reveal_red));


        } else {
            ((CellFeedViewHolder) holder).timeEndDay.setTextColor(context.getResources().getColor(R.color.reveal_yellow));
            ((CellFeedViewHolder) holder).timeEndHour.setTextColor(context.getResources().getColor(R.color.reveal_yellow));
            ((CellFeedViewHolder) holder).timeEndMinute.setTextColor(context.getResources().getColor(R.color.reveal_yellow));
            ((CellFeedViewHolder) holder).timeEndDay.setText(timeEnd[0] + "");
            ((CellFeedViewHolder) holder).timeEndHour.setText(timeEnd[1] + "");
            ((CellFeedViewHolder) holder).timeEndMinute.setText(timeEnd[2] + "");
        }

        String content = "";
        if (n.getContent().length()>60){
            content = n.getContent().substring(0,60);
        } else {
            content = n.getContent();
        }
        ((CellFeedViewHolder) holder).note.setText(content);

        for (int i = 0; i < 5; i++) {
            ((CellFeedViewHolder) holder).checkBoxEnd[i].setChecked(false);

        }

        switch (n.getCheckCount()) {
            case 5:
                ((CellFeedViewHolder) holder).checkBoxEnd[0].setChecked(true);
                ((CellFeedViewHolder) holder).checkBoxEnd[0].setVisibility(View.VISIBLE);
            case 4:
                ((CellFeedViewHolder) holder).checkBoxEnd[1].setChecked(true);
                ((CellFeedViewHolder) holder).checkBoxEnd[1].setVisibility(View.VISIBLE);
            case 3:
                ((CellFeedViewHolder) holder).checkBoxEnd[2].setChecked(true);
                ((CellFeedViewHolder) holder).checkBoxEnd[2].setVisibility(View.VISIBLE);
            case 2:
                ((CellFeedViewHolder) holder).checkBoxEnd[3].setChecked(true);
                ((CellFeedViewHolder) holder).checkBoxEnd[3].setVisibility(View.VISIBLE);
            case 1:
                ((CellFeedViewHolder) holder).checkBoxEnd[4].setChecked(true);
                ((CellFeedViewHolder) holder).checkBoxEnd[4].setVisibility(View.VISIBLE);
        }
    }


    @Override
    public int getItemCount() {
        return items.size();
    }

    @Override
    public void onClick(View v) {
        if (onRecyclerViewItemClickListener != null) {
            onRecyclerViewItemClickListener.onItemClick(v, (int) v.getTag());
        }
    }

    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
        this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }

    class CellFeedViewHolder extends RecyclerView.ViewHolder {
        TextView note;
        TextView timeEndDay;
        TextView timeEndHour;
        TextView timeEndMinute;
        TextView time_end_Day;
        TextView time_end_hour;
        TextView time_end_minute;
        TextView time_end_last;

        CheckBox[] checkBoxEnd = new CheckBox[5];

        public CellFeedViewHolder(View itemView) {
            super(itemView);
            note = (TextView) itemView.findViewById(R.id.tv);
            timeEndDay = (TextView) itemView.findViewById(R.id.time_end_day);
            timeEndHour = (TextView) itemView.findViewById(R.id.time_end_hour);
            timeEndMinute = (TextView) itemView.findViewById(R.id.time_end_minute);

            time_end_last = (TextView) itemView.findViewById(R.id.time_end_1);
            time_end_Day = (TextView) itemView.findViewById(R.id.time_end_2);
            time_end_hour = (TextView) itemView.findViewById(R.id.time_end_3);
            time_end_minute = (TextView) itemView.findViewById(R.id.time_end_4);

            checkBoxEnd[0] = (CheckBox) itemView.findViewById(R.id.checkbox_1_end);
            checkBoxEnd[1] = (CheckBox) itemView.findViewById(R.id.checkbox_2_end);
            checkBoxEnd[2] = (CheckBox) itemView.findViewById(R.id.checkbox_3_end);
            checkBoxEnd[3] = (CheckBox) itemView.findViewById(R.id.checkbox_4_end);
            checkBoxEnd[4] = (CheckBox) itemView.findViewById(R.id.checkbox_5_end);
        }
    }

    public interface OnRecyclerViewItemClickListener {
        void onItemClick(View v, int position);
    }

    private long[] timeBetween(int[] date) {
        long[] time = new long[3];
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String systemTime = (sdf.format(new Date())).toString();
        String aimTime = date[0] + "-" + date[1] + "-" + date[2] + " " + date[3] + ":" + date[4] + ":" + date[5];


        Date begin = null;
        Date end = null;
        try {
            begin = sdf.parse(systemTime);
            end = sdf.parse(aimTime);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (end.after(begin)) {
            long between = (end.getTime() - begin.getTime()) / 1000;//转换成秒

            long day = between / (24 * 3600);
            long hour = between % (24 * 3600) / 3600;
            long minute = between % 3600 / 60;

            time[0] = day;
            time[1] = hour;
            time[2] = minute;//分钟存在误差

            return time;

        } else {
            return new long[]{-1, -1, -1};
        }
    }

}
